<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>前言 | 数据挖掘项目</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/rossmann/assets/logo.jpg">
    <meta name="description" content="Data Mining">
    
    <link rel="preload" href="/rossmann/assets/css/0.styles.96fad13f.css" as="style"><link rel="preload" href="/rossmann/assets/js/app.4768928e.js" as="script"><link rel="preload" href="/rossmann/assets/js/2.46bed51d.js" as="script"><link rel="preload" href="/rossmann/assets/js/13.d8f675b5.js" as="script"><link rel="prefetch" href="/rossmann/assets/js/10.9ada15e0.js"><link rel="prefetch" href="/rossmann/assets/js/11.f29b186a.js"><link rel="prefetch" href="/rossmann/assets/js/12.d00b63a5.js"><link rel="prefetch" href="/rossmann/assets/js/3.c508cade.js"><link rel="prefetch" href="/rossmann/assets/js/4.2be134af.js"><link rel="prefetch" href="/rossmann/assets/js/5.11a4289f.js"><link rel="prefetch" href="/rossmann/assets/js/6.fb1784b8.js"><link rel="prefetch" href="/rossmann/assets/js/7.d79025f2.js"><link rel="prefetch" href="/rossmann/assets/js/8.bc6b450a.js"><link rel="prefetch" href="/rossmann/assets/js/9.a786ec9b.js">
    <link rel="stylesheet" href="/rossmann/assets/css/0.styles.96fad13f.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/rossmann/" class="home-link router-link-active"><!----> <span class="site-name">数据挖掘项目</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/rossmann/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/rossmann/guide/" class="nav-link router-link-active">
  技术文档
</a></div><div class="nav-item"><a href="https://vimeriochen.gitee.io/visualization/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  具体效果
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="http://sim.whu.edu.cn/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情链接
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/rossmann/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/rossmann/guide/" class="nav-link router-link-active">
  技术文档
</a></div><div class="nav-item"><a href="https://vimeriochen.gitee.io/visualization/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  具体效果
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="http://sim.whu.edu.cn/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情链接
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>echarts</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>vue-press</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>数据挖掘</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/rossmann/guide/数据挖掘 一.html" class="sidebar-link">前言</a></li><li><a href="/rossmann/guide/数据挖掘 二.html" class="active sidebar-link">前言</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/rossmann/guide/数据挖掘 二.html#前言" class="sidebar-link">前言</a></li><li class="sidebar-sub-header"><a href="/rossmann/guide/数据挖掘 二.html#四-特征工程" class="sidebar-link">四.特征工程</a></li><li class="sidebar-sub-header"><a href="/rossmann/guide/数据挖掘 二.html#_2-数据类型转换" class="sidebar-link">2.数据类型转换</a></li><li class="sidebar-sub-header"><a href="/rossmann/guide/数据挖掘 二.html#_3-特征处理" class="sidebar-link">3.特征处理</a></li><li class="sidebar-sub-header"><a href="/rossmann/guide/数据挖掘 二.html#五-建立模型" class="sidebar-link">五.建立模型</a></li><li class="sidebar-sub-header"><a href="/rossmann/guide/数据挖掘 二.html#六-补充参数调优相关步骤" class="sidebar-link">六.补充参数调优相关步骤</a></li><li class="sidebar-sub-header"><a href="/rossmann/guide/数据挖掘 二.html#七-项目总结与反思" class="sidebar-link">七.项目总结与反思</a></li></ul></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h2 id="前言"><a href="#前言" class="header-anchor">#</a> <strong>前言</strong></h2> <p>本文紧接上一篇。通过特征工程包括onehot处理，标准化处理，将特征数据转化为模型可以测试的数据。最后建模过程，本次案例主要用到的是<strong>线性回归</strong>，<strong>决策树</strong>和<strong>随机森林</strong>和<strong>GBDT</strong>四类回归算法。</p> <h2 id="四-特征工程"><a href="#四-特征工程" class="header-anchor">#</a> <strong>四.特征工程</strong></h2> <p><strong>1.检查和处理异常值</strong></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>data_train<span class="token punctuation">.</span>head<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029192736909.png" alt="image-20211029192736909"></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>data_test<span class="token punctuation">.</span>head<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token comment">#上文一开始就导入的test表数据</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029192749797.png" alt="image-20211029192749797"></p> <p>发现test数据没有顾客数，需要删除</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#删除顾客数一列</span>
data_train<span class="token operator">=</span>data_train<span class="token punctuation">.</span>drop<span class="token punctuation">(</span><span class="token string">'Customers'</span><span class="token punctuation">,</span>axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
<span class="token comment">#对data_test先做和data_train之前相同的变化</span>
data_test<span class="token punctuation">.</span>StateHoliday<span class="token operator">=</span>data_test<span class="token punctuation">.</span>StateHoliday<span class="token punctuation">.</span><span class="token builtin">map</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">'0'</span><span class="token punctuation">:</span><span class="token string">'无'</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">:</span><span class="token string">'无'</span><span class="token punctuation">,</span><span class="token string">'a'</span><span class="token punctuation">:</span><span class="token string">'公共假日'</span><span class="token punctuation">,</span><span class="token string">'b'</span><span class="token punctuation">:</span><span class="token string">'复活节假期'</span><span class="token punctuation">,</span><span class="token string">'c'</span><span class="token punctuation">:</span><span class="token string">'圣诞节'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token comment">#连接test表和train表</span>
data_train_test<span class="token operator">=</span>pd<span class="token punctuation">.</span>concat<span class="token punctuation">(</span><span class="token punctuation">[</span>data_train<span class="token punctuation">,</span>data_test<span class="token punctuation">.</span>drop<span class="token punctuation">(</span><span class="token string">'Id'</span><span class="token punctuation">,</span>axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">,</span>axis<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>ignore_index<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
data_train_test<span class="token punctuation">.</span>sample<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029210417403.png" alt="image-20211029210417403"></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#连接store表和train_test表</span>
data_train_test<span class="token operator">=</span>pd<span class="token punctuation">.</span>merge<span class="token punctuation">(</span>data_train_test<span class="token punctuation">,</span>data_store<span class="token punctuation">,</span>on<span class="token operator">=</span><span class="token string">'Store'</span><span class="token punctuation">,</span>how<span class="token operator">=</span><span class="token string">'left'</span><span class="token punctuation">)</span>
data_train_test<span class="token punctuation">.</span>head<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029210431508.png" alt="image-20211029210431508"></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#将时间类型转换</span>
data_train_test<span class="token punctuation">.</span>Date<span class="token operator">=</span>pd<span class="token punctuation">.</span>to_datetime<span class="token punctuation">(</span>data_train_test<span class="token punctuation">.</span>Date<span class="token punctuation">)</span>
data_train_test<span class="token punctuation">.</span>info<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029210445274.png" alt="image-20211029210445274"></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#把日期时间分解</span>
data_train_test<span class="token punctuation">[</span><span class="token string">'year'</span><span class="token punctuation">]</span><span class="token operator">=</span>data_train_test<span class="token punctuation">.</span>Date<span class="token punctuation">.</span><span class="token builtin">map</span><span class="token punctuation">(</span><span class="token keyword">lambda</span> x<span class="token punctuation">:</span>x<span class="token punctuation">.</span>year<span class="token punctuation">)</span>
data_train_test<span class="token punctuation">[</span><span class="token string">'month'</span><span class="token punctuation">]</span><span class="token operator">=</span>data_train_test<span class="token punctuation">.</span>Date<span class="token punctuation">.</span><span class="token builtin">map</span><span class="token punctuation">(</span><span class="token keyword">lambda</span> x<span class="token punctuation">:</span>x<span class="token punctuation">.</span>month<span class="token punctuation">)</span>
data_train_test<span class="token punctuation">[</span><span class="token string">'day'</span><span class="token punctuation">]</span><span class="token operator">=</span>data_train_test<span class="token punctuation">.</span>Date<span class="token punctuation">.</span><span class="token builtin">map</span><span class="token punctuation">(</span><span class="token keyword">lambda</span> x<span class="token punctuation">:</span>x<span class="token punctuation">.</span>day<span class="token punctuation">)</span>
data_train_test<span class="token punctuation">[</span><span class="token string">'Date'</span><span class="token punctuation">]</span><span class="token operator">=</span>data_train_test<span class="token punctuation">.</span>Date<span class="token punctuation">.</span><span class="token builtin">map</span><span class="token punctuation">(</span><span class="token keyword">lambda</span> x<span class="token punctuation">:</span>x<span class="token punctuation">.</span>date<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#检查空值</span>
check_none<span class="token punctuation">(</span>data_train_test<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029210541249.png" alt="image-20211029210541249"></p> <p><strong>发现open列有空值，因为之前对train表做过空值处理，因此该空值来源test表，因为open直接决定销售额的有无，所以需要谨慎补空值</strong></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#检查open列空值情况</span>
<span class="token builtin">sum</span><span class="token punctuation">(</span>data_train_test<span class="token punctuation">.</span>Open<span class="token punctuation">.</span>isnull<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token number">11</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>发现open空值一共11行</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#查看这11行的缺失情况</span>
data_train_test<span class="token punctuation">[</span>data_train_test<span class="token punctuation">.</span>Open<span class="token punctuation">.</span>isnull<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029210600137.png" alt="image-20211029210600137"></p> <p>可以发现都是store622缺失，首先搞促销是肯定营业的，所以先对其进行修改</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>data_train_test<span class="token punctuation">.</span>loc<span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1017688</span><span class="token punctuation">,</span><span class="token number">1018544</span><span class="token punctuation">,</span><span class="token number">1019400</span><span class="token punctuation">,</span><span class="token number">1020256</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token string">'Open'</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span>
data_train_test<span class="token punctuation">.</span>loc<span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1017688</span><span class="token punctuation">,</span><span class="token number">1018544</span><span class="token punctuation">,</span><span class="token number">1019400</span><span class="token punctuation">,</span><span class="token number">1020256</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token comment">#检查是否改正成功</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029210612374.png" alt="image-20211029210612374"></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#找出空值的日期，作图看该日期其他店的营运情况</span>
date_null<span class="token operator">=</span>data_train_test<span class="token punctuation">[</span>data_train_test<span class="token punctuation">.</span>Open<span class="token punctuation">.</span>isnull<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>Date
<span class="token comment">#查看这个日期下其他店铺是否营业情况</span>
data_date_null<span class="token operator">=</span>data_train_test<span class="token punctuation">[</span>data_train_test<span class="token punctuation">.</span>Date<span class="token punctuation">.</span>isin<span class="token punctuation">(</span>date_null<span class="token punctuation">)</span><span class="token punctuation">]</span>
df12<span class="token operator">=</span>data_date_null<span class="token punctuation">.</span>groupby<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'Date'</span><span class="token punctuation">,</span><span class="token string">'Open'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">'Store'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>count<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>rename<span class="token punctuation">(</span><span class="token string">'count'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>reset_index<span class="token punctuation">(</span><span class="token punctuation">)</span>

fig<span class="token operator">=</span>plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
ax<span class="token operator">=</span>fig<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>barplot<span class="token punctuation">(</span><span class="token string">'Date'</span><span class="token punctuation">,</span><span class="token string">'count'</span><span class="token punctuation">,</span>hue<span class="token operator">=</span><span class="token string">'Open'</span><span class="token punctuation">,</span>data<span class="token operator">=</span>df12<span class="token punctuation">,</span>ax<span class="token operator">=</span>ax<span class="token punctuation">)</span>
ax<span class="token punctuation">.</span>set_xticklabels<span class="token punctuation">(</span>df12<span class="token punctuation">.</span>Date<span class="token punctuation">,</span>rotation<span class="token operator">=</span><span class="token number">45</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029210627764.png" alt="image-20211029210627764"></p> <p>可以知道这几天基本都营业，基本排除不营业的特殊日子情况，所以可以也将其修改为营业</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>index_null<span class="token operator">=</span>data_train_test<span class="token punctuation">[</span>data_train_test<span class="token punctuation">.</span>Open<span class="token punctuation">.</span>isnull<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>index
data_train_test<span class="token punctuation">.</span>loc<span class="token punctuation">[</span>index_null<span class="token punctuation">,</span><span class="token string">'Open'</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span>
<span class="token builtin">sum</span><span class="token punctuation">(</span>data_train_test<span class="token punctuation">.</span>Open<span class="token punctuation">.</span>isnull<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">#查看是否补全空值</span>

<span class="token number">0</span>


<span class="token comment">#检查每列的空值行数</span>
<span class="token keyword">def</span> <span class="token function">check_none_col</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span>column<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'{}的缺失行数：'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>column<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token builtin">sum</span><span class="token punctuation">(</span>data<span class="token punctuation">[</span>column<span class="token punctuation">]</span><span class="token punctuation">.</span>isnull<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'{}的缺失率：'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>column<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token builtin">round</span><span class="token punctuation">(</span><span class="token builtin">sum</span><span class="token punctuation">(</span>data<span class="token punctuation">[</span>column<span class="token punctuation">]</span><span class="token punctuation">.</span>isnull<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">/</span>data<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
columns<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'CompetitionDistance'</span><span class="token punctuation">,</span><span class="token string">'CompetitionOpenSinceMonth'</span><span class="token punctuation">,</span><span class="token string">'CompetitionOpenSinceYear'</span><span class="token punctuation">,</span>
         <span class="token string">'Promo2SinceWeek'</span><span class="token punctuation">,</span><span class="token string">'Promo2SinceYear'</span><span class="token punctuation">,</span><span class="token string">'PromoInterval'</span><span class="token punctuation">]</span>   
<span class="token keyword">for</span> i <span class="token keyword">in</span> columns<span class="token punctuation">:</span>
    check_none_col<span class="token punctuation">(</span>data_store<span class="token punctuation">,</span>i<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029210703806.png" alt="image-20211029210703806"></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#查看缺失列与sales的相关关系</span>
data_tem<span class="token operator">=</span>pd<span class="token punctuation">.</span>concat<span class="token punctuation">(</span><span class="token punctuation">[</span>data_train_test<span class="token punctuation">[</span>columns<span class="token punctuation">]</span><span class="token punctuation">,</span>data_train_test<span class="token punctuation">.</span>Sales<span class="token punctuation">]</span><span class="token punctuation">,</span>axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
data_tem<span class="token punctuation">.</span>corr<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029210716243.png" alt="image-20211029210716243"></p> <p>可以看出这几列缺失对于销售额影响非常小，因此可以通过众数或平均值填补。</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#备份数据，以防改错数据</span>
data_safe<span class="token operator">=</span>data_train_test
<span class="token comment">#平均值填补距离</span>
data_train_test<span class="token punctuation">.</span>CompetitionDistance<span class="token operator">=</span>data_train_test<span class="token punctuation">.</span>CompetitionDistance<span class="token punctuation">.</span>fillna<span class="token punctuation">(</span>data_train_test<span class="token punctuation">.</span>CompetitionDistance<span class="token punctuation">.</span>mean<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#众数填补其他</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> columns<span class="token punctuation">:</span>
    data_train_test<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>data_train_test<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>fillna<span class="token punctuation">(</span>data_train_test<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>mode<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">#mode返回series，不是一个数</span>
check_none<span class="token punctuation">(</span>data_train_test<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029210737471.png" alt="image-20211029210737471"></p> <p>空值都已经填补完，剩下的空值为需要预测的sales</p> <h2 id="_2-数据类型转换"><a href="#_2-数据类型转换" class="header-anchor">#</a> 2.数据类型转换</h2> <div class="language-python line-numbers-mode"><pre class="language-python"><code>data_train_test<span class="token punctuation">.</span>dtypes
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029210755372.png" alt="image-20211029210755372"></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#浮点数转为整数</span>
<span class="token keyword">def</span> <span class="token function">convert_to_int</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span>columns<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> columns<span class="token punctuation">:</span>
        data<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>data<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>astype<span class="token punctuation">(</span>np<span class="token punctuation">.</span>int64<span class="token punctuation">)</span>
    <span class="token keyword">return</span> data
columns_float<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'Open'</span><span class="token punctuation">,</span><span class="token string">'CompetitionOpenSinceMonth'</span><span class="token punctuation">,</span><span class="token string">'CompetitionOpenSinceYear'</span><span class="token punctuation">,</span><span class="token string">'Promo2SinceWeek'</span><span class="token punctuation">,</span><span class="token string">'Promo2SinceYear'</span><span class="token punctuation">]</span>
data_train_test<span class="token operator">=</span>convert_to_int<span class="token punctuation">(</span>data_train_test<span class="token punctuation">,</span>columns_float<span class="token punctuation">)</span>
data_train_test<span class="token punctuation">.</span>dtypes
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029210806901.png" alt="image-20211029210806901"></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#将数值数据转化为分类数据</span>
<span class="token keyword">def</span> <span class="token function">convert_to_object</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span>columns<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> columns<span class="token punctuation">:</span>
        data<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>data<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>astype<span class="token punctuation">(</span>np<span class="token punctuation">.</span><span class="token builtin">str</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> data
columns_classify<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'DayOfWeek'</span><span class="token punctuation">,</span><span class="token string">'CompetitionOpenSinceMonth'</span><span class="token punctuation">,</span><span class="token string">'CompetitionOpenSinceYear'</span><span class="token punctuation">,</span><span class="token string">'Promo2SinceWeek'</span><span class="token punctuation">,</span><span class="token string">'Promo2SinceYear'</span><span class="token punctuation">,</span><span class="token string">'year'</span><span class="token punctuation">,</span><span class="token string">'month'</span><span class="token punctuation">,</span><span class="token string">'day'</span><span class="token punctuation">]</span>
data_train_test<span class="token operator">=</span>convert_to_object<span class="token punctuation">(</span>data_train_test<span class="token punctuation">,</span>columns_classify<span class="token punctuation">)</span>
data_train_test<span class="token punctuation">.</span>dtypes
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029210827386.png" alt="image-20211029210827386"></p> <h2 id="_3-特征处理"><a href="#_3-特征处理" class="header-anchor">#</a> 3.特征处理</h2> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#查看数据唯一值数量</span>
check_unique<span class="token punctuation">(</span>data_train_test<span class="token punctuation">)</span><span class="token comment">#该函数见上篇数据处理部分</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>Date唯一值数量: 990
DayOfWeek唯一值数量: 7
Open唯一值数量: 2
Promo唯一值数量: 2
Sales唯一值数量: 21735
SchoolHoliday唯一值数量: 2
StateHoliday唯一值数量: 4
Store唯一值数量: 1115
StoreType唯一值数量: 4
Assortment唯一值数量: 3
CompetitionDistance唯一值数量: 655
CompetitionOpenSinceMonth唯一值数量: 12
CompetitionOpenSinceYear唯一值数量: 23
Promo2唯一值数量: 2
Promo2SinceWeek唯一值数量: 24
Promo2SinceYear唯一值数量: 7
PromoInterval唯一值数量: 3
year唯一值数量: 3
month唯一值数量: 12
day唯一值数量: 31</p> <p><strong>发现PromoInterval只有3个数，查看情况</strong></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code>data_store<span class="token punctuation">[</span><span class="token string">'PromoInterval'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>value_counts<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>Jan,Apr,Jul,Oct 335
Feb,May,Aug,Nov 130
Mar,Jun,Sept,Dec 106
Name: PromoInterval, dtype: int64</p> <p><strong>可以得出做prom2的都是间隔3个月，所以一共只有3种分类</strong></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#分类数据二值化处理</span>
<span class="token keyword">def</span> <span class="token function">convert_to_twovalues</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span>columns<span class="token punctuation">)</span><span class="token punctuation">:</span>
    connect_column<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> columns<span class="token punctuation">:</span>
        df_tem<span class="token operator">=</span>pd<span class="token punctuation">.</span>get_dummies<span class="token punctuation">(</span>data<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span>prefix<span class="token operator">=</span>i<span class="token punctuation">)</span>
        connect_column<span class="token punctuation">.</span>append<span class="token punctuation">(</span>df_tem<span class="token punctuation">)</span>
    data_new<span class="token operator">=</span>pd<span class="token punctuation">.</span>concat<span class="token punctuation">(</span>connect_column<span class="token punctuation">,</span>axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> data_new
columns_value_processing<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'DayOfWeek'</span><span class="token punctuation">,</span><span class="token string">'StateHoliday'</span><span class="token punctuation">,</span><span class="token string">'StoreType'</span><span class="token punctuation">,</span><span class="token string">'Assortment'</span><span class="token punctuation">,</span><span class="token string">'CompetitionOpenSinceMonth'</span><span class="token punctuation">,</span>
                          <span class="token string">'CompetitionOpenSinceYear'</span><span class="token punctuation">,</span><span class="token string">'Promo2SinceWeek'</span><span class="token punctuation">,</span><span class="token string">'Promo2SinceYear'</span><span class="token punctuation">,</span><span class="token string">'PromoInterval'</span><span class="token punctuation">,</span><span class="token string">'year'</span><span class="token punctuation">,</span><span class="token string">'month'</span><span class="token punctuation">,</span><span class="token string">'day'</span><span class="token punctuation">]</span>
data_value_processing<span class="token operator">=</span>convert_to_twovalues<span class="token punctuation">(</span>data_train_test<span class="token punctuation">,</span>columns_value_processing<span class="token punctuation">)</span>
data_value_processing<span class="token punctuation">.</span>head<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029210910744.png" alt="image-20211029210910744"></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#对一行多个值进行二值化处理,使用每行值是多个值的</span>
<span class="token comment">#之前以为‘PromoInterval’需要这样处理所以做了这个函数，本次项目不需要，可保留作为以后项目备用</span>
<span class="token keyword">def</span> <span class="token function">convert_to_twovalues_more</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span>column<span class="token punctuation">)</span><span class="token punctuation">:</span>
    month<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> data<span class="token punctuation">[</span>column<span class="token punctuation">]</span><span class="token punctuation">:</span>
        month<span class="token punctuation">.</span>extend<span class="token punctuation">(</span>i<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">','</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">#注意要把字符串变成列表</span>
    index_list<span class="token operator">=</span>pd<span class="token punctuation">.</span>Index<span class="token punctuation">(</span><span class="token builtin">list</span><span class="token punctuation">(</span><span class="token builtin">set</span><span class="token punctuation">(</span>month<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>index_list<span class="token punctuation">)</span>
    data_new<span class="token operator">=</span>pd<span class="token punctuation">.</span>DataFrame<span class="token punctuation">(</span>np<span class="token punctuation">.</span>zeros<span class="token punctuation">(</span><span class="token punctuation">[</span>data<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token builtin">len</span><span class="token punctuation">(</span>index_list<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span>columns<span class="token operator">=</span>index_list<span class="token punctuation">)</span>
    <span class="token keyword">for</span> i<span class="token punctuation">,</span>data <span class="token keyword">in</span> <span class="token builtin">enumerate</span><span class="token punctuation">(</span>data<span class="token punctuation">[</span>column<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        column_position<span class="token operator">=</span>index_list<span class="token punctuation">.</span>get_indexer<span class="token punctuation">(</span>data<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">','</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        data_new<span class="token punctuation">.</span>iloc<span class="token punctuation">[</span>i<span class="token punctuation">,</span>column_position<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span>
    <span class="token keyword">return</span> data_new
<span class="token comment">#直接运行速度很慢，所以现在data_store修改再连接,此代码留到以后使用</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><div class="language-text line-numbers-mode"><pre class="language-text"><code>#数值类型进行归一化(0-1范围)
def feature_standarize(data,columns):
    combine_col=[]
    for j in columns:
        min_=data[j].min()
        max_=data[j].max()
        standard_col=data[j].apply(lambda x:(x-min_)/(max_-min_))#标准化为0-1范围
        #standard_col=data[j].apply(lambda x:(x-data[j].min())/(data[j].max()-data[j].min()))这样运行很慢，因为每次都要找min，max
        combine_col.append(standard_col)
    data_new2=pd.concat(combine_col,axis=1)
    return data_new2

data_CompetitionDistance=feature_standarize(data_train_test,['CompetitionDistance'])
data_CompetitionDistance.head()
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029210935830.png" alt="image-20211029210935830"></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#连接数据data_value_processing，data_CompetitionDistance，data_train_test未作修改的列</span>

data_train_test_new<span class="token operator">=</span>pd<span class="token punctuation">.</span>concat<span class="token punctuation">(</span><span class="token punctuation">[</span>data_value_processing<span class="token punctuation">,</span>data_CompetitionDistance<span class="token punctuation">,</span>data_train_test<span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token string">'Open'</span><span class="token punctuation">,</span><span class="token string">'Promo'</span><span class="token punctuation">,</span><span class="token string">'SchoolHoliday'</span><span class="token punctuation">,</span><span class="token string">'Promo2'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
data_train_test_new<span class="token punctuation">.</span>info<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029210950554.png" alt="image-20211029210950554"></p> <h2 id="五-建立模型"><a href="#五-建立模型" class="header-anchor">#</a> <strong>五.建立模型</strong></h2> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#将数据拆分为训练_测试数据和预测数据</span>
index_split<span class="token operator">=</span>data_train<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">-</span><span class="token number">1</span>
data_train_test_final<span class="token operator">=</span>pd<span class="token punctuation">.</span>concat<span class="token punctuation">(</span><span class="token punctuation">[</span>data_train_test_new<span class="token punctuation">.</span>loc<span class="token punctuation">[</span><span class="token punctuation">:</span>index_split<span class="token punctuation">]</span><span class="token punctuation">,</span>data_train<span class="token punctuation">[</span><span class="token string">'Sales'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
data_for_predict<span class="token operator">=</span>data_train_test_new<span class="token punctuation">.</span>loc<span class="token punctuation">[</span>data_train<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">:</span><span class="token punctuation">]</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>data_train_test_final<span class="token punctuation">.</span>info<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">#打印训练测试数据信息</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>data_for_predict<span class="token punctuation">.</span>info<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">#打印预测数据信息</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029211001260.png" alt="image-20211029211001260"></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#做模型前先看看各变量与销售额的相关关系</span>
corr<span class="token operator">=</span>data_train_test_final<span class="token punctuation">.</span>corr<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">'Sales'</span><span class="token punctuation">]</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'{:*^30}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span><span class="token string">'正相关前10的列'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>corr<span class="token punctuation">.</span>sort_values<span class="token punctuation">(</span>ascending<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'{:*^30}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span><span class="token string">'负相关前10的列'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>corr<span class="token punctuation">.</span>sort_values<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><em><strong><strong><strong><strong><strong>正相关前10的列</strong></strong></strong></strong></strong></em>
Sales 1.000000
Open 0.678472
Promo 0.452345
StateHoliday_无 0.254216
DayOfWeek_1 0.215309
StoreType_b 0.139940
DayOfWeek_2 0.130764
DayOfWeek_5 0.100895
SchoolHoliday 0.085124
DayOfWeek_3 0.083047
Name: Sales, dtype: float64
<em><strong><strong><strong><strong><strong>负相关前10的列</strong></strong></strong></strong></strong></em>
DayOfWeek_7 -0.589219
StateHoliday_公共假日 -0.203028
StateHoliday_复活节假期 -0.117497
StateHoliday_圣诞节 -0.092618
Promo2 -0.091040
Assortment_a -0.080494
Promo2SinceYear_2013 -0.079153
day_1 -0.053450
PromoInterval_Mar,Jun,Sept,Dec -0.053267
day_25 -0.045535
Name: Sales, dtype: float64</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#导入模型</span>
<span class="token keyword">from</span> sklearn<span class="token punctuation">.</span>linear_model <span class="token keyword">import</span> LinearRegression
<span class="token keyword">from</span> sklearn<span class="token punctuation">.</span>ensemble <span class="token keyword">import</span> GradientBoostingRegressor
<span class="token keyword">from</span> sklearn<span class="token punctuation">.</span>tree <span class="token keyword">import</span> DecisionTreeRegressor
<span class="token keyword">from</span> sklearn<span class="token punctuation">.</span>ensemble <span class="token keyword">import</span> RandomForestRegressor
<span class="token keyword">from</span> sklearn<span class="token punctuation">.</span>model_selection <span class="token keyword">import</span> GridSearchCV<span class="token punctuation">,</span>cross_val_score<span class="token punctuation">,</span>StratifiedKFold<span class="token punctuation">,</span>train_test_split
<span class="token keyword">from</span> sklearn<span class="token punctuation">.</span>metrics <span class="token keyword">import</span> mean_squared_error<span class="token punctuation">,</span>r2_score
<span class="token comment">#拆分数据为训练数据和测试数据</span>
data_x<span class="token operator">=</span>data_train_test_final<span class="token punctuation">.</span>iloc<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
data_y<span class="token operator">=</span>data_train_test_final<span class="token punctuation">.</span>iloc<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span>
train_x<span class="token punctuation">,</span>test_x<span class="token punctuation">,</span>train_y<span class="token punctuation">,</span>test_y<span class="token operator">=</span>train_test_split<span class="token punctuation">(</span>data_x<span class="token punctuation">,</span>data_y<span class="token punctuation">,</span>test_size<span class="token operator">=</span><span class="token number">0.2</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>train_x<span class="token punctuation">.</span>shape<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>train_y<span class="token punctuation">.</span>shape<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>test_x<span class="token punctuation">.</span>shape<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>test_y<span class="token punctuation">.</span>shape<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>(813767, 138)
(813767, 1)
(203442, 138)
(203442, 1)</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">import</span> warnings
warnings<span class="token punctuation">.</span>filterwarnings<span class="token punctuation">(</span><span class="token string">&quot;ignore&quot;</span><span class="token punctuation">)</span>

<span class="token comment">#用cross_val_score交叉检验各模型的评分</span>
lr_model<span class="token operator">=</span>LinearRegression<span class="token punctuation">(</span><span class="token punctuation">)</span>
tree_model<span class="token operator">=</span>DecisionTreeRegressor<span class="token punctuation">(</span><span class="token punctuation">)</span>
gbdt_model<span class="token operator">=</span>GradientBoostingRegressor<span class="token punctuation">(</span><span class="token punctuation">)</span>
rfr_model<span class="token operator">=</span>RandomForestRegressor<span class="token punctuation">(</span><span class="token punctuation">)</span>
models<span class="token operator">=</span><span class="token punctuation">[</span>lr_model<span class="token punctuation">,</span>tree_model<span class="token punctuation">,</span>rfr_model<span class="token punctuation">,</span>gbdt_model<span class="token punctuation">]</span>
model_names<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'lr_model'</span><span class="token punctuation">,</span><span class="token string">'tree_model'</span><span class="token punctuation">,</span><span class="token string">'rfr_model'</span><span class="token punctuation">,</span><span class="token string">'gbdt_model'</span><span class="token punctuation">]</span>
scores<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> model<span class="token punctuation">,</span>model_name <span class="token keyword">in</span> <span class="token builtin">zip</span><span class="token punctuation">(</span>models<span class="token punctuation">,</span>model_names<span class="token punctuation">)</span><span class="token punctuation">:</span>
    t5<span class="token operator">=</span>time<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span>
    score<span class="token operator">=</span>cross_val_score<span class="token punctuation">(</span>model<span class="token punctuation">,</span>train_x<span class="token punctuation">,</span>train_y<span class="token punctuation">,</span>cv<span class="token operator">=</span>StratifiedKFold<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    t6<span class="token operator">=</span>time<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'{}运行时间：'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>model_name<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span>t6<span class="token operator">-</span>t5<span class="token punctuation">)</span><span class="token punctuation">)</span>
    scores<span class="token punctuation">.</span>append<span class="token punctuation">(</span>score<span class="token punctuation">)</span>
score_matrix<span class="token operator">=</span>pd<span class="token punctuation">.</span>DataFrame<span class="token punctuation">(</span>scores<span class="token punctuation">,</span>index<span class="token operator">=</span>model_names<span class="token punctuation">)</span>
score_matrix<span class="token punctuation">[</span><span class="token string">'mean'</span><span class="token punctuation">]</span><span class="token operator">=</span>score_matrix<span class="token punctuation">.</span>mean<span class="token punctuation">(</span>axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
score_matrix<span class="token punctuation">[</span><span class="token string">'std'</span><span class="token punctuation">]</span><span class="token operator">=</span>score_matrix<span class="token punctuation">.</span>std<span class="token punctuation">(</span>axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'{:*^30}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span><span class="token string">'各模型分数矩阵'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>score_matrix<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><p>lr_model运行时间： 379.4038813114166
tree_model运行时间： 762.0898385047913
rfr_model运行时间： 2077.197418689728
gbdt_model运行时间： 10079.884629249573
<em><strong><strong><strong><strong><strong>各模型分数矩阵</strong></strong></strong></strong></strong></em>*
0 1 2 3 4 mean <br>
lr_model 0.532610 0.603956 0.631439 0.644842 0.651042 0.612778
tree_model 0.889164 0.898869 0.895752 0.897552 0.893235 0.894914
rfr_model 0.917106 0.938303 0.938530 0.938050 0.937982 0.933994
gbdt_model 0.572033 0.653069 0.680165 0.696969 0.699075 0.660262</p> <p>std
lr_model 0.043232
tree_model 0.003440
rfr_model 0.008447
gbdt_model 0.047087</p> <p><strong>可以发现集成算法模型运行时间特别长gbdt几乎花了3个小时检验；从评分角度看出决策树和随机森林的预测效果更好</strong></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#对测试数据进行预测和实际结果对比，并通过mse和r2评分，将分数保存到scores3里</span>
scores3<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> model<span class="token punctuation">,</span>model_name <span class="token keyword">in</span> <span class="token builtin">zip</span><span class="token punctuation">(</span>models<span class="token punctuation">,</span>model_names<span class="token punctuation">)</span><span class="token punctuation">:</span>
    model<span class="token punctuation">.</span>fit<span class="token punctuation">(</span>train_x<span class="token punctuation">,</span>train_y<span class="token punctuation">)</span>
    y_pred<span class="token operator">=</span>model<span class="token punctuation">.</span>predict<span class="token punctuation">(</span>test_x<span class="token punctuation">)</span>
    mse<span class="token operator">=</span>mean_squared_error<span class="token punctuation">(</span>test_y<span class="token punctuation">,</span>y_pred<span class="token punctuation">)</span>
    r2<span class="token operator">=</span>r2_score<span class="token punctuation">(</span>test_y<span class="token punctuation">,</span>y_pred<span class="token punctuation">)</span>
    scores3<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token punctuation">[</span>mse<span class="token punctuation">,</span>r2<span class="token punctuation">]</span><span class="token punctuation">)</span>

<span class="token comment">#作图查看测试数据预测和实际拟合程度，对测试数据预测数据间隔400取数，这样图能看的更清晰</span>
y_lr_predict<span class="token operator">=</span>lr_model<span class="token punctuation">.</span>predict<span class="token punctuation">(</span>test_x<span class="token punctuation">)</span>
y_tree_predict<span class="token operator">=</span>tree_model<span class="token punctuation">.</span>predict<span class="token punctuation">(</span>test_x<span class="token punctuation">)</span>
y_rfr_predict<span class="token operator">=</span>rfr_model<span class="token punctuation">.</span>predict<span class="token punctuation">(</span>test_x<span class="token punctuation">)</span>
y_gbdt_predict<span class="token operator">=</span>gbdt_model<span class="token punctuation">.</span>predict<span class="token punctuation">(</span>test_x<span class="token punctuation">)</span>

fig<span class="token operator">=</span>plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">30</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>style<span class="token punctuation">.</span>use<span class="token punctuation">(</span><span class="token string">'ggplot'</span><span class="token punctuation">)</span>
predicts<span class="token operator">=</span><span class="token punctuation">[</span>y_lr_predict<span class="token punctuation">,</span>y_tree_predict<span class="token punctuation">,</span>y_rfr_predict<span class="token punctuation">,</span>y_gbdt_predict<span class="token punctuation">]</span>
colors<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'r'</span><span class="token punctuation">,</span><span class="token string">'g'</span><span class="token punctuation">,</span><span class="token string">'y'</span><span class="token punctuation">,</span><span class="token string">'b'</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> predict<span class="token punctuation">,</span>model_name<span class="token punctuation">,</span>color <span class="token keyword">in</span> <span class="token builtin">zip</span><span class="token punctuation">(</span>predicts<span class="token punctuation">,</span>model_names<span class="token punctuation">,</span>colors<span class="token punctuation">)</span><span class="token punctuation">:</span>
    plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>predict<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token number">500</span><span class="token punctuation">]</span><span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>color<span class="token operator">=</span>color<span class="token punctuation">,</span>label<span class="token operator">=</span><span class="token string">'{}预测'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>model_name<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">#间隔400取值</span>
plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>test_y<span class="token punctuation">.</span>loc<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token number">500</span><span class="token punctuation">,</span><span class="token string">'Sales'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>values<span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token string">'k'</span><span class="token punctuation">,</span>label<span class="token operator">=</span><span class="token string">'实际'</span><span class="token punctuation">)</span>    
<span class="token comment">#range(test_y.shape[0]),</span>
<span class="token comment">#test_y.plot(ax=ax,color='k',label='')</span>
plt<span class="token punctuation">.</span>legend<span class="token punctuation">(</span>loc<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#将模型评分矩阵化展示</span>
metrics_matrix<span class="token operator">=</span>pd<span class="token punctuation">.</span>DataFrame<span class="token punctuation">(</span>scores3<span class="token punctuation">,</span>index<span class="token operator">=</span>model_names<span class="token punctuation">,</span>columns<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'mse'</span><span class="token punctuation">,</span><span class="token string">'r2'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'{:*^30}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span><span class="token string">'各模型分数矩阵'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>metrics_matrix<span class="token punctuation">)</span>
<span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">*</span>各模型分数矩阵<span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span>
                     mse        r2
lr_model    <span class="token number">5.760669e+06</span>  <span class="token number">0.610438</span>
tree_model  <span class="token number">1.470425e+06</span>  <span class="token number">0.900563</span>
rfr_model   <span class="token number">9.156802e+05</span>  <span class="token number">0.938078</span>
gbdt_model  <span class="token number">5.052725e+06</span>  <span class="token number">0.658312</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>可以看出对于预测数据依旧是决策树和随机森林效果更好，图形上也能看出红色线（回归模型预测）总是会偏出比较多。</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#查看决策树过程特征的重要程度</span>
importance_metrics<span class="token operator">=</span>pd<span class="token punctuation">.</span>Series<span class="token punctuation">(</span>tree_model<span class="token punctuation">.</span>feature_importances_<span class="token punctuation">,</span>index<span class="token operator">=</span>train_x<span class="token punctuation">.</span>columns<span class="token punctuation">)</span><span class="token punctuation">.</span>sort_values<span class="token punctuation">(</span><span class="token punctuation">)</span>
fig<span class="token operator">=</span>plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>style<span class="token punctuation">.</span>use<span class="token punctuation">(</span><span class="token string">'ggplot'</span><span class="token punctuation">)</span>
ax<span class="token operator">=</span>fig<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
importance_metrics<span class="token punctuation">[</span>importance_metrics<span class="token operator">&gt;</span><span class="token number">0.005</span><span class="token punctuation">]</span><span class="token punctuation">.</span>plot<span class="token punctuation">.</span>barh<span class="token punctuation">(</span>ax<span class="token operator">=</span>ax<span class="token punctuation">)</span><span class="token comment">#帅选重要程度超过0.005的</span>
ax<span class="token punctuation">.</span>set_xlabel<span class="token punctuation">(</span><span class="token string">'重要性程度'</span><span class="token punctuation">)</span>
ax<span class="token punctuation">.</span>set_title<span class="token punctuation">(</span><span class="token string">'各特征重要程度分析'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/image-20211029223438232.png" alt="image-20211029223438232"></p> <p>从图中可以发现一个问题，在之前做相关性的分析时发现竞争对手相关列对于销售额的相关程度不高，但是从此图可以看出包括竞争距离，竞争者建立的时间在特征重要程度上排的比较前，这也说明之前只是对于竞争者信息列的缺失处理可能不佳，对预测结果也有一定影响。同时关联性分析排名和特征重要程度的排名也是有不少区别，这可以在以后的需恶习中注意这点。</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#保存决策树规则图</span>
<span class="token keyword">import</span> pydotplus
<span class="token keyword">from</span> sklearn <span class="token keyword">import</span> tree
dot_data<span class="token operator">=</span>tree<span class="token punctuation">.</span>export_graphviz<span class="token punctuation">(</span>decision_tree<span class="token operator">=</span>tree_model<span class="token punctuation">,</span>max_depth<span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">,</span>feature_names<span class="token operator">=</span>train_x<span class="token punctuation">.</span>columns<span class="token punctuation">,</span>
                              filled<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>rounded<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>out_file<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">)</span><span class="token comment">#out_file控制不生成dot对象，否则dot_data为空</span>
graph<span class="token operator">=</span>pydotplus<span class="token punctuation">.</span>graph_from_dot_data<span class="token punctuation">(</span>dot_data<span class="token punctuation">)</span>
graph<span class="token punctuation">.</span>write_pdf<span class="token punctuation">(</span><span class="token string">r'C:\Users\Administrator.DESKTOP-ULJ84AO\Desktop\1\tree.pdf'</span><span class="token punctuation">)</span>
<span class="token boolean">True</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><img src="https://gitee.com/vimeriochen/gitee-pages-imgs/raw/master/tree_00.jpg" alt="tree_00"></p> <p><strong>总结：从整体来看集成算法（gbdt和随机森林）会比相应的单个算法（简单线性回归和决策树）的小效果好，对于四个模型的差别比较大，个人认为对于特征多为二分类，以决策树为基础模型的模型效果会更好，而gbdt一开始的预测模型是线性回归，而对于预测与实际的偏差才用决策树，这样虽然结果比简单线性回归好，但总体的预测是由线性回归决定，因此也只能在线性回归基础上提高而已，由于本人对机场算法研究不是很多，个人觉得如果gbdt这类模型的一开始先用决策树做初步预测，后续再对预测与实际差距进行预测，这样的话效果可能比一定比随机森林差，这需要以后学习拓展。</strong></p> <h2 id="六-补充参数调优相关步骤"><a href="#六-补充参数调优相关步骤" class="header-anchor">#</a> <strong>六.补充参数调优相关步骤</strong></h2> <p>因为参数调优化的时间非常多，本人为了节省时间，对一些参数进行逐一调优观察具体哪个参数所画的时间比较久，本次补充只作步骤展示，结果对项目结果没有印象</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#为减少数据量，只选取前20重要的特征数据</span>
col<span class="token operator">=</span>importance_metrics<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">20</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">.</span>index
train_x<span class="token punctuation">[</span>col<span class="token punctuation">]</span><span class="token punctuation">.</span>head<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
<span class="token comment">#参数调优</span>
gbdt_model2<span class="token operator">=</span>GradientBoostingRegressor<span class="token punctuation">(</span><span class="token punctuation">)</span>
rfr_model2<span class="token operator">=</span>RandomForestRegressor<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">#建立调优函数</span>
<span class="token keyword">def</span> <span class="token function">param_adjust</span><span class="token punctuation">(</span>model<span class="token punctuation">,</span>params<span class="token punctuation">,</span>x_trian<span class="token punctuation">,</span>y_train<span class="token punctuation">)</span><span class="token punctuation">:</span>
    gscv<span class="token operator">=</span>GridSearchCV<span class="token punctuation">(</span>estimator<span class="token operator">=</span>model<span class="token punctuation">,</span>param_grid<span class="token operator">=</span>params<span class="token punctuation">,</span>cv<span class="token operator">=</span><span class="token number">4</span><span class="token punctuation">)</span>
    gscv<span class="token punctuation">.</span>fit<span class="token punctuation">(</span>x_trian<span class="token punctuation">,</span>y_train<span class="token punctuation">)</span>
    <span class="token keyword">return</span> gscv<span class="token punctuation">.</span>best_score_<span class="token punctuation">,</span>gscv<span class="token punctuation">.</span>best_params_<span class="token punctuation">,</span>gscv<span class="token punctuation">.</span>best_estimator_


params_gbdt<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">'loss'</span><span class="token punctuation">:</span><span class="token punctuation">[</span><span class="token string">'ls'</span><span class="token punctuation">,</span><span class="token string">'lad'</span><span class="token punctuation">,</span><span class="token string">'huber'</span><span class="token punctuation">,</span><span class="token string">'quantile'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token string">'alpha'</span><span class="token punctuation">:</span>np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">0.1</span><span class="token punctuation">,</span><span class="token number">1.0</span><span class="token punctuation">,</span><span class="token number">0.2</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
           <span class="token string">'min_samples_leaf'</span><span class="token punctuation">:</span>np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
           <span class="token string">'n_estimators'</span><span class="token punctuation">:</span><span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            <span class="token string">'max_depth'</span><span class="token punctuation">:</span><span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">)</span>
            <span class="token punctuation">}</span>
<span class="token comment">#对每个参数逐一调优观察时间</span>
<span class="token keyword">for</span> key<span class="token punctuation">,</span>value <span class="token keyword">in</span> params_gbdt<span class="token punctuation">.</span>items<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    t1<span class="token operator">=</span>time<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span>
    gbdt_best_score_<span class="token punctuation">,</span>gbdt_best_params_<span class="token punctuation">,</span>gbdt_best_estimator_<span class="token operator">=</span>param_adjust<span class="token punctuation">(</span>gbdt_model2<span class="token punctuation">,</span><span class="token builtin">dict</span><span class="token punctuation">(</span><span class="token builtin">zip</span><span class="token punctuation">(</span><span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span>value<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span>train_x<span class="token punctuation">[</span>col<span class="token punctuation">]</span><span class="token punctuation">,</span>train_y<span class="token punctuation">)</span>
    t2<span class="token operator">=</span>time<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'{}最佳参数：'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">,</span>gbdt_best_params_<span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'{}最佳分数：'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">,</span>gbdt_best_score_<span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'gbdt调参所花时间：'</span><span class="token punctuation">,</span><span class="token punctuation">(</span>t2<span class="token operator">-</span>t1<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><p>loss最佳参数： {'loss': 'ls'}
loss最佳分数： 0.6484665168776873
gbdt调参所花时间： 1257.0478587150574
alpha最佳参数： {'alpha': 0.30000000000000004}
alpha最佳分数： 0.6484665168776873
gbdt调参所花时间： 1384.6650688648224
min_samples_leaf最佳参数： {'min_samples_leaf': 50}
min_samples_leaf最佳分数： 0.6484835100791693
gbdt调参所花时间： 1401.155752658844
n_estimators最佳参数： {'n_estimators': 90}
n_estimators最佳分数： 0.6439393883257161
gbdt调参所花时间： 755.3742513656616</p> <p><strong>由于max_depth时间很长都没有结果，因此提前结束程序，可以看出即使数据量从130多列变成20列，并且每次只对一个参数调优，运行的时间也很长。同时也可以看出参数调优的变化对分数变化并不会很大，因此之前的模型即使没有参数调优，效果也不会差很多。</strong></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment">#将数据量减少为十列，同时只提取2014年的数据，将行数也砍唯一大半</span>
train_x_new<span class="token operator">=</span>train_x<span class="token punctuation">.</span>reset_index<span class="token punctuation">(</span>drop<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
train_x_new<span class="token operator">=</span>train_x_new<span class="token punctuation">.</span>ix<span class="token punctuation">[</span>train_x_new<span class="token punctuation">.</span>year_2014<span class="token operator">==</span><span class="token number">1</span><span class="token punctuation">,</span>col2<span class="token punctuation">]</span>
index<span class="token operator">=</span>train_x_new<span class="token punctuation">.</span>index
train_y_new<span class="token operator">=</span>train_y<span class="token punctuation">.</span>reset_index<span class="token punctuation">(</span>drop<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">.</span>iloc<span class="token punctuation">[</span>index<span class="token punctuation">,</span><span class="token punctuation">:</span><span class="token punctuation">]</span>

params_rfr<span class="token operator">=</span><span class="token punctuation">{</span>
           <span class="token string">'min_samples_leaf'</span><span class="token punctuation">:</span><span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
           <span class="token string">'max_depth'</span><span class="token punctuation">:</span><span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">50</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
           <span class="token string">'n_estimators'</span><span class="token punctuation">:</span><span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token number">200</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">)</span>
            <span class="token punctuation">}</span>

t3<span class="token operator">=</span>time<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">#多参数一起调优</span>
rfr_best_score_<span class="token punctuation">,</span>rfr_best_params_<span class="token punctuation">,</span>rfr_best_estimator_<span class="token operator">=</span>param_adjust<span class="token punctuation">(</span>rfr_model2<span class="token punctuation">,</span>params_rfr<span class="token punctuation">,</span>train_x_new<span class="token punctuation">,</span>train_y_new<span class="token punctuation">)</span>
t4<span class="token operator">=</span>time<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'{}最佳参数：'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">,</span>rfr_best_params_<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'{}最佳分数：'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">,</span>rfr_best_score_<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'rfr调参所花时间：'</span><span class="token punctuation">,</span><span class="token punctuation">(</span>t4<span class="token operator">-</span>t3<span class="token punctuation">)</span><span class="token punctuation">)</span>
   
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><p>min_samples_leaf最佳参数： {'max_depth': 40, 'min_samples_leaf': 10, 'n_estimators': 180}
min_samples_leaf最佳分数： 0.8306565841587666
rfr调参所花时间： 35315.951684474945</p> <p><strong>可以看出多参数一起调优，即使数据列已经只有十列，行数也从80万较少为20多万，运行时间也要接近10个小时，因为电脑负荷太大影响其他工作就不用该功能了。</strong></p> <h2 id="七-项目总结与反思"><a href="#七-项目总结与反思" class="header-anchor">#</a> <strong>七.项目总结与反思</strong></h2> <p>1.本次项目对于竞争对手信息，包括距离，开店时间的缺失补全因为没有考虑更多实际业务，所以补全结果不佳会对最终预测会有一定影响。
2.同时由于数据量太大，对数据调参部分也只是展示一个步骤，并未运用到实际模型预测，所以相关模型也未达到最佳效果。
3.项目由于数据量大，有很多步骤包括标准化过程都需要进行代码优化和函数运用，减少重复步骤带来的时间消耗，这也是处理大型数据的重要方法。
4.没有对异常数据处理，一般情况可以对销售额超过分布的三个标准偏差的数据去调再建模，因为每条信息的销售额特别高或特别低可能是有一定原因的，如果预测的数据也正好满足这类特殊情况，那去掉异常值可能也会对某些预测结果不佳。个人觉得其实可以单独把异常数据和非异常数据找出来分别做分析来看最后的拟合效果，篇幅有限就不做探究.</p> <p>5.在不同情况所用的算法模型往往不同，如果考虑时间因素，集成算法可能无需考虑。而对于特征情况，如果特征多是以数值类数据而不是分类数据出现，并且这些数值特征对结果有一定影响，则线性回归可能会比决策树类模型要好，这需要以后正对具体情况进行实验</p></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/rossmann/guide/数据挖掘 一.html" class="prev">
        前言
      </a></span> <!----></p></div> </main></div><div class="global-ui"></div></div>
    <script src="/rossmann/assets/js/app.4768928e.js" defer></script><script src="/rossmann/assets/js/2.46bed51d.js" defer></script><script src="/rossmann/assets/js/13.d8f675b5.js" defer></script>
  </body>
</html>
